<template>
  <el-row :gutter="20" class="status-panel">
    <el-col :span="12">
      <el-card shadow="hover" class="status-card">
        <div class="card-content">
          <div class="left-icon">
            <el-icon class="car-icon"><Van /></el-icon>
          </div>
          <div class="right-info">
            <div class="title">当前在厂车辆</div>
            <div class="count">{{ inCount }}</div>
          </div>
        </div>
      </el-card>
    </el-col>

    <el-col :span="12">
      <el-card shadow="hover" class="status-card">
        <div class="card-content">
          <div class="left-icon">
            <el-icon class="parking-icon"><Opportunity /></el-icon>
          </div>
          <div class="right-info">
            <div class="title">剩余车位</div>
            <div
              class="count"
              :style="{ color: availableSpaces <= 10 ? '#f56c6c' : '#67c23a' }"
            >
              {{ availableSpaces }}
            </div>
          </div>
        </div>
      </el-card>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Van, Opportunity } from '@element-plus/icons-vue'
import api from '@/api'

const inCount = ref(0)
const totalSpaces = ref(200) // 总车位可配置，建议后续从接口获取

// 计算剩余车位
const availableSpaces = computed(() => {
  return Math.max(totalSpaces.value - inCount.value, 0)
})

// 获取实时状态
const fetchStatus = async () => {
  try {
    const res = await api.getCurrentStatus()
    inCount.value = res.data.in_count || 0
    // 如果接口返回总车位则更新
    if (res.data.total_spaces) {
      totalSpaces.value = res.data.total_spaces
    }
  } catch (err) {
    ElMessage.error(`状态获取失败: ${err.message}`)
  }
}

// 初始化加载+定时刷新
onMounted(() => {
  fetchStatus()
  setInterval(fetchStatus, 3000) // 每3秒刷新
})
</script>

<style scoped>
.status-panel {
  margin-bottom: 20px;
}

.status-card {
  border-radius: 8px;
  transition: transform 0.3s;
}

.status-card:hover {
  transform: translateY(-3px);
}

.card-content {
  display: flex;
  align-items: center;
  padding: 10px;
}

.left-icon {
  margin-right: 20px;
}

.left-icon .el-icon {
  font-size: 40px;
  padding: 10px;
  border-radius: 50%;
}

.car-icon {
  color: #409EFF;
  background: rgba(64, 158, 255, 0.1);
}

.parking-icon {
  color: #67C23A;
  background: rgba(103, 194, 58, 0.1);
}

.right-info {
  flex: 1;
}

.title {
  color: #909399;
  font-size: 14px;
  margin-bottom: 5px;
}

.count {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
}
</style>
